package com.qqhf.hitfood.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qqhf.cache.AccountService;
import com.qqhf.common.entity.AjaxResult;
import com.qqhf.common.entity.PageEntity;
import com.qqhf.hitfood.bean.core.BaseController;
import com.qqhf.hitfood.bean.entity.AcctAdmin;
import com.qqhf.hitfood.bean.entity.AcctInfo;
import com.qqhf.hitfood.service.IAcctAdminService;
import com.qqhf.security.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "A - 管理员")
@RestController
@RequestMapping("/acct/admin")
public class AcctAdminController extends BaseController {
    @Autowired
    private IAcctAdminService acctAdminService;
    @Autowired
    private AccountService accountService;

    @Operation(summary = "分页查询管理员")
    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, AcctAdmin acctAdmin) {
        return AjaxResult.success(acctAdminService.page(pageEntity.toPage(), Wrappers.lambdaQuery(acctAdmin)));
    }

    @Operation(summary = "返回管理员清单")
    @GetMapping("/list")
    public AjaxResult list(AcctAdmin acctAdmin) {
        return AjaxResult.success(acctAdminService.list(Wrappers.lambdaQuery(acctAdmin)));
    }

    @Operation(summary = "添加管理员")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AcctAdmin acctAdmin) {
        return AjaxResult.success(acctAdminService.save(acctAdmin));
    }

    @Operation(summary = "编辑管理员")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody AcctAdmin acctAdmin) {
        return AjaxResult.success(acctAdminService.updateById(acctAdmin));
    }

    @Operation(summary = "ids查询管理员")
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(acctAdminService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "id查询管理员")

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(acctAdminService.getById(id));
    }

    @Operation(summary = "管理员登录")
    @PostMapping(value = "/login")
    public AjaxResult login(
            @RequestParam("roleId") Integer roleId,
            @RequestParam("username") String name,
            @RequestParam("password") String password) {
        AcctAdmin acctAdmin = acctAdminService.getOne(new QueryWrapper<AcctAdmin>().eq("name", name).eq("role_id", roleId), false);
        if (acctAdmin == null) {
            return AjaxResult.error("该用户不存在");
        }
        if (!acctAdmin.getPassword().equals(password)) {
            return AjaxResult.error("输入的密码错误");
        }
        if (acctAdmin.getStId() == 2) {
            return AjaxResult.error("该账号已冻结");
        }
        if (acctAdmin.getStId() == 3) {
            return AjaxResult.error("该账号已停用");
        }
        Map<String, Object> result = new HashMap<>();
        AcctInfo acctInfo = new AcctInfo();
        BeanUtils.copyProperties(acctAdmin, acctInfo);
        String token = JwtUtil.sign(acctInfo);
        result.put("token", token);
        if (roleId == 2) {
            result.put("shopId", acctAdmin.getShopId());
        }
        return AjaxResult.success(result);
    }

    @Operation(summary = "管理员展示信息")
    @GetMapping(value = "/info")
    public AjaxResult info(@RequestParam("token") String token) {
        AcctInfo acctInfo = JwtUtil.getAcctInfo(token);
        Integer id = acctInfo.getId();
        if (id != null) {
            return AjaxResult.success(acctInfo);
        }
        return AjaxResult.error("获取用户信息失败");
    }

    @Operation(summary = "退出登陆")
    @PostMapping(value = "/logout")
    public Object logout(HttpServletRequest request) {
        String token = this.getToken(request);
        accountService.logout(token);
        return AjaxResult.success();
    }
}
